import { request } from '../../request/index.js'
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      //左侧菜单
      leftMenulist:[],
      //右侧商品
      rightMenulist:[],
      //被选中的菜单索引
      currentIndex:0,
      scrollTop:0
  },
  //存放所有数据
  Cates:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.getGoodsList()
    const Cates = wx.getStorageSync('cates');
    if(!Cates){
      //如果本地存储内没有缓存，发送请求
      this.getGoodsList();
    }else{
      if(Date.now()-Cates.time>1000*10){
          this.getGoodsList()
      }else{
        this.Cates = Cates.data;
        let leftmenu = this.Cates.map(v=>v.cat_name);
        let rightmenu = this.Cates[0].children;
        this.setData({
          leftMenulist:leftmenu,
          rightMenulist:rightmenu
        })
      }
    }
      
  },
  //分区商品数据
  async getGoodsList(){
    // request({
    //   url:'/categories'
    // }).then(result=>{
    //   this.Cates = result;
    //   wx.setStorageSync('cates', {time:Date.now(),data:this.Cates});
    //  // console.log(this.Cates);
    //   let leftmenu = this.Cates.map(v=>v.cat_name);
    //   let rightmenu = this.Cates[0].children;
    //   this.setData({
    //     leftMenulist:leftmenu,
    //     rightMenulist:rightmenu
    //   })
    // })
    const result =await request({url:'/categories'})
    //console.log(result);
    this.Cates = result;
    wx.setStorageSync('cates', {time:Date.now(),data:this.Cates});
   // console.log(this.Cates);
    let leftmenu = this.Cates.map(v=>v.cat_name);
    let rightmenu = this.Cates[0].children;
    this.setData({
      leftMenulist:leftmenu,
      rightMenulist:rightmenu
    })
    
  },
  //点击切换事件
  changeItem(e){
    // console.log(e);
    const {index} = e.currentTarget.dataset;
    let rightmenu = this.Cates[index].children;
    this.setData({
      currentIndex:index,
      rightMenulist:rightmenu,
      scrollTop:0
    })
  }
})